<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\CollectionService;
use App\Services\CourseService;
use App\Services\TeachingAidsService;
use App\Services\TeachingAidService;
use Session;

class CollectionController extends Controller{
	public function list($mid){
		if(!$mid){
			$_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
		}else{
			$collection = new CollectionService;
			$course = new CourseService;
			$teachingAids = new TeachingAidsService;
			$teachingAid = new TeachingAidService;

			$collectionlist = $collection->getCollectionList($mid);

			if(!empty($collectionlist)){
                foreach($collectionlist['course_list'] as $ctm=>$cst){
                    $collectionlist['course_list'][$ctm]->courseinfo = $course->getCourseInfoById($cst->product_id);
                }

                foreach($collectionlist['teaching_list'] as $clm=>$clt){
                    if($clt->type == 'realias'){
                        $collectionlist['teaching_list'][$clm]->productinfo = $teachingAids->getTaInfoById
                        ($clt->product_id);
                    }else{
                        $collectionlist['teaching_list'][$clm]->productinfo = $teachingAid->getAidInfoById
                        ($clt->product_id);
                    }
                }

				$_data = array('code'=>'2x000001','list'=>$collectionlist);
			}else{
				$_data = array('code'=>'4x000001','msg'=>'暂无收藏信息');
			}
		}

		return response()->json($_data);
	}

	public function store(){
		$collection = new CollectionService;

		$data['member_id'] = intval($_POST['member_id']);
		$data['address'] = addslashes($_POST['address']);
		$data['add_date'] = date('Y-m-d H:i:s');

		if(!$data['member_id']){
			$_data = array('code'=>'3x000001','msg'=>'请登录');
		}else{
			$collectioninfo = $collection->getCollectionInfo($data['member_id'],$data['address']);
			if(!empty($collectioninfo)){
				$_data = array('code'=>'3x000002','msg'=>'已收藏,请勿重复收藏');
			}else{
				$res = $collection->insertCollection($data);
				if($res){
					$_data = array('code'=>'2x000001','msg'=>'收藏成功');
				}else{
					$_data = array('code'=>'4x000001','msg'=>'收藏失败');
				}
			}
		}

		return response()->json($_data);
	}

	public function del($id){
		$collection = new CollectionService;

		$res = $collection->delCollection($id);
		if($res){
			$_data = array('code'=>'2x000001','msg'=>'取消收藏成功');
		}else{
			$_data = array('code'=>'4x000001','msg'=>'取消收藏失败');
		}

		return response()->json($_data);
	}
}